package com.dhcc.sdc.cdss.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.dhcc.cdc.busi.dao.CdcOrdmastMapper;
import com.dhcc.cdc.busi.entity.CdcDictionaryEntity;
import com.dhcc.cdc.busi.entity.CdcOrdmastEntity;
import com.dhcc.cdc.busi.service.ICdcDictionaryService;
import com.dhcc.sdc.cdss.dao.SdcCdssTreamatchMapper;
import com.dhcc.sdc.cdss.dao.SdcCdssTreatMapper;
import com.dhcc.sdc.cdss.entity.SdcCdssTreamatchEntity;
import com.dhcc.sdc.cdss.entity.SdcCdssTreatEntity;
import com.dhcc.sdc.cdss.service.ISdcCdssTreamatchService;
import com.dhcc.sds.standard.dto.CdcOrdmastDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


/**
 * 表数据服务层接口
 * 
 * @ClassName: SdcCdssTreamatchServiceImpl
 * @Description: TODO
 * @author zhangdc
 * @date 2022-06-23 16:04:20
 */
@Service("sdcCdssTreamatchService")
public class SdcCdssTreamatchServiceImpl extends BaseServiceImpl<SdcCdssTreamatchMapper, SdcCdssTreamatchEntity> implements ISdcCdssTreamatchService {

	@Resource
	private SdcCdssTreatMapper cdssTreatMapper;
	@Resource
	private CdcOrdmastMapper cdcOrdmastMapper;
	@Autowired
	private ICdcDictionaryService cdcDictionaryService;

	@Override
	public List<SdcCdssTreamatchEntity> page(Page<SdcCdssTreamatchEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<SdcCdssTreamatchEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public List<CdcOrdmastDto> pageMatchOrder(Page<SdcCdssTreamatchEntity> page, Map<String, Object> params) {
		List<CdcOrdmastDto> cdcOrdmastDtoList = CollUtil.newArrayList();
		String id = (String) params.get("id");
		String orderKey = "";
		if (!StringUtils.isEmpty(params.get("orderKey"))){
			orderKey = (String) params.get("orderKey");
		}
		SdcCdssTreatEntity cdssTreatEntity = cdssTreatMapper.selectById(Long.parseLong(id));
		if (!StringUtils.isEmpty(cdssTreatEntity)){
			params.put("itemId", cdssTreatEntity.getItemId());
		}else {
			return null;
		}

		//默认查询已对照的
		List<SdcCdssTreamatchEntity> page1 = this.baseMapper.page(page, params);
		if (null != page1 && page1.size() > 0){
			for (SdcCdssTreamatchEntity cdssTreamatchEntity : page1){
				CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
				CdcOrdmastEntity cdcOrdmastEntity = cdcOrdmastMapper.selectById(cdssTreamatchEntity.getOrderId());
				if (!StringUtils.isEmpty(orderKey) && !cdcOrdmastEntity.getDescription().contains(orderKey)){
					continue;
				}
				BeanUtil.copyProperties(cdcOrdmastEntity,cdcOrdmastDto);
				cdcOrdmastDto.setIsDefault(cdssTreamatchEntity.getIsDefault());
				if (!StringUtils.isEmpty(cdcOrdmastEntity.getBusOrderCateId())){
					CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryService.selectById(cdcOrdmastEntity.getBusOrderCateId());
					if (!StringUtils.isEmpty(cdcDictionaryEntity)){
						cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription());
					}
					if (!StringUtils.isEmpty(cdcOrdmastEntity.getBusOrderSubcateId())){
						CdcDictionaryEntity cdcDictionaryEntity2 = cdcDictionaryService.selectById(cdcOrdmastEntity.getBusOrderSubcateId());
						if (!StringUtils.isEmpty(cdcDictionaryEntity2)){
							cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription() + "." + cdcDictionaryEntity2.getDescription());
						}
					}
				}
				cdcOrdmastDto.setChecked(1);
				cdcOrdmastDtoList.add(cdcOrdmastDto);
			}
		}

		return cdcOrdmastDtoList;
	}

	@Override
	public List<CdcOrdmastDto> pageOrder(Page<CdcOrdmastEntity> page, Map<String, Object> params) {
		List<CdcOrdmastDto> res = CollUtil.newArrayList();
		String compare = (String) params.get("compare");
		String id = (String) params.get("id");
		String orderKey = "";
		if (!StringUtils.isEmpty(params.get("orderKey"))){
			orderKey = (String) params.get("orderKey");
		}
		params.put("description", orderKey);
		SdcCdssTreatEntity cdssTreatEntity = cdssTreatMapper.selectById(Long.parseLong(id));
		if (!StringUtils.isEmpty(cdssTreatEntity)){
			params.put("itemId", cdssTreatEntity.getItemId());
		}else {
			return null;
		}

		if (Objects.equals("all", compare)){
			//查询所有
			List<SdcCdssTreamatchEntity> list = this.baseMapper.list(params);
			List<CdcOrdmastEntity> page1 = cdcOrdmastMapper.page(page, params);

			for (CdcOrdmastEntity cdcOrdmastEntity : page1){
				CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
				BeanUtil.copyProperties(cdcOrdmastEntity,cdcOrdmastDto);
				if (!StringUtils.isEmpty(cdcOrdmastEntity.getBusOrderCateId())){
					CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryService.selectById(cdcOrdmastEntity.getBusOrderCateId());
					if (!StringUtils.isEmpty(cdcDictionaryEntity)){
						cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription());
					}
					if (!StringUtils.isEmpty(cdcOrdmastEntity.getBusOrderSubcateId())){
						CdcDictionaryEntity cdcDictionaryEntity2 = cdcDictionaryService.selectById(cdcOrdmastEntity.getBusOrderSubcateId());
						if (!StringUtils.isEmpty(cdcDictionaryEntity2)){
							cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription() + "." + cdcDictionaryEntity2.getDescription());
						}
					}
				}
				res.add(cdcOrdmastDto);
			}

			if (list.size() == 0){
				return res;
			}else {
				for (CdcOrdmastDto cdcOrdmastDto : res){
					for (SdcCdssTreamatchEntity cdssTreamatchEntity : list) {
						if (Objects.equals(cdcOrdmastDto.getId(), cdssTreamatchEntity.getOrderId())) {
							cdcOrdmastDto.setChecked(1);
							cdcOrdmastDto.setIsDefault(cdssTreamatchEntity.getIsDefault());
						}
					}
				}
				return res;
			}
		}else if (Objects.equals("noMatch", compare)){
			//查询未对照的
			List<Long> matchOrdmastIds = this.baseMapper.selectMatchOrdmastIds(params);
			List<CdcOrdmastEntity> cdcOrdmastEntities = cdcOrdmastMapper.pageExcludeMatch(page, params, matchOrdmastIds);
			for (CdcOrdmastEntity cdcOrdmastEntity : cdcOrdmastEntities){
				CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
				BeanUtil.copyProperties(cdcOrdmastEntity,cdcOrdmastDto);
				if (!StringUtils.isEmpty(cdcOrdmastEntity.getBusOrderCateId())){
					CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryService.selectById(cdcOrdmastEntity.getBusOrderCateId());
					if (!StringUtils.isEmpty(cdcDictionaryEntity)){
						cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription());
					}
					if (!StringUtils.isEmpty(cdcOrdmastEntity.getBusOrderSubcateId())){
						CdcDictionaryEntity cdcDictionaryEntity2 = cdcDictionaryService.selectById(cdcOrdmastEntity.getBusOrderSubcateId());
						if (!StringUtils.isEmpty(cdcDictionaryEntity2)){
							cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription() + "." + cdcDictionaryEntity2.getDescription());
						}
					}
				}
				res.add(cdcOrdmastDto);
			}
		}

		return res;
	}

}